package com.android.quizvoctrainer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import android.text.TextUtils;

public class QuizVocList {
	private final CharSequence d = Utils.delimitor;
	private final CharSequence nl = "\n";
	private final int maxPop = 1000;
	private CharSequence filename = "";
	private CharSequence name = "";
	private CharSequence title = "";
	private QuizVocItem[] items = new QuizVocItem[]{};
	private List<QuizVocItem> popSortedQuizVocItems = new ArrayList<QuizVocItem>();
	
	public CharSequence getFilename() {return filename;};
	public CharSequence getName() {return name;};
	public CharSequence getTitle() {return title;};
	public QuizVocItem getItem(int popIndex) {return popSortedQuizVocItems.get(popIndex);}
	public void setFilename(CharSequence value) {filename = value;};
	public void setName(CharSequence value) {name = value;};
	public void setTitle(CharSequence value) {title = value;};
	public int getLength() {
		return items.length;
	}
	public int setResultImpact(boolean ok, int popIndex) {
		QuizVocItem item = popSortedQuizVocItems.get(popIndex);
		CharSequence stats = item.getStats();
		if (stats.length() == 20) stats = stats.subSequence(1, 20);
		if (ok) item.setStats(stats + "1");
		else item.setStats(stats + "0");
		item.setLastDateTime(System.currentTimeMillis());
		int popOffset = 0;
		if (!ok) {
			// bad answer, pop is down by 2 * number of bad answers in stats
			popOffset = popOffset - 4 * item.stats.toString().replaceAll("1", "").length();
		} else {
			int factor = item.stats.length(); // If no bad answer, factor is number of (good = total) answers
			// getting number of last non interrupted good answer
			int i = item.stats.toString().lastIndexOf('0');
			if (i > -1) {
				// at least one bad answer
				// we count the number of good answer in the last X answers
				// X = number of last non interrupted good answers * 2
				i = item.stats.length() - i - 1; // i is now the number of last non interrupted good answers
				if (i * 2 >= item.stats.length()) {
					factor = item.stats.toString().replaceAll("0", "").length();
				} else {
					factor = item.stats.subSequence(item.stats.length() - i * 2, item.stats.length()).toString().replaceAll("0", "").length();
				}
			}
			popOffset = popOffset + (int)(Math.pow(2,factor));
		}
		item.popularity = item.popularity + popOffset;
		if (item.popularity > maxPop) item.popularity = maxPop;
		for (int i=0;i < items.length;i++) {
			if (items[i].popularity > 0) items[i].popularity --;
		}
		return popOffset;
	}
	public void sortListByPop() {
		Collections.sort(popSortedQuizVocItems);
	}
	public String toString() {
		StringBuffer sb = new StringBuffer(name);
		sb.append(nl).append(title);
		for (int i=0;i < items.length;i++) {
			sb.append(nl).append(items[i].toCharSequence());
		}
		return sb.toString();
	}
	public void fromString(String value) {
		String[] lines = TextUtils.split(value, nl.toString());
		setName("");
		setTitle("");
		items = new QuizVocItem[]{};
		if (lines.length > 0) {
			setName(lines[0]);
			if (lines.length > 1) {
				setTitle(lines[1]);
				if (lines.length > 2) {
					items = new QuizVocItem[lines.length - 2];
					for (int i=2;i < lines.length;i++) {
						items[i-2] = new QuizVocItem(i-2,lines[i]);
					}
				}
			}
		}
		popSortedQuizVocItems = Arrays.asList(items);
		Collections.sort(popSortedQuizVocItems);
	}
	
	public class QuizVocItem implements Comparable<QuizVocItem> {
		private int index = -1;
		private CharSequence question = "";
		private CharSequence answer = "";
		private CharSequence stats = "";
		private long lastDateTime = System.currentTimeMillis();
		private long popularity = 0;
		public QuizVocItem(int _index, String value) {
			super();
			this.fromString(_index, value);
		}
		public int compareTo(QuizVocItem item) {
			if (popularity > item.popularity) return 1;
			else if (popularity < item.popularity) return -1;
			else if (lastDateTime > item.lastDateTime) return 1;
			else if (lastDateTime < item.lastDateTime) return -1;
			else return 0;
		}
		public int getIndex() {return index;};
		public CharSequence getQuestion() {return question;};
		public CharSequence getAnwser() {return answer;};
		public CharSequence getStats() {return stats;};
		public long getLastDateTime() {return lastDateTime;};
		public long getPopularity() {return popularity;};
		public void setIndex(int value) {index = value;};
		public void setQuestion(CharSequence value) {question = value;};
		public void setAnswer(CharSequence value) {answer = value;};
		public void setStats(CharSequence value) {stats = value;};
		public void setLastDateTime(long value) {lastDateTime = value;};
		public void setPopularity(long value) {popularity = value;};
		public CharSequence toCharSequence() {
			return TextUtils.concat(question,d,answer,d,stats,d,(CharSequence)(lastDateTime + ""),d,(CharSequence)(popularity + ""));
		}
		public void fromString(int _index, String value) {
			index = _index;
			String[] splitted = TextUtils.split(value, (String) d);
			question = splitted[0];
			answer = splitted[1];
			if (splitted.length > 2) stats = splitted[2];
			else stats = "";
			if (splitted.length > 3) lastDateTime = new Long(splitted[3]).longValue();
			else lastDateTime = System.currentTimeMillis();
			if (splitted.length > 4) popularity = new Long(splitted[4]).longValue();
			else popularity = index * 2;
		}
	}
}
